import configparser
import utils
import os
import json
import requests
import utils


# 微信登录
class WechatLogin:

    def __init__(self, gh_id, pwd, url='https://mp.weixin.qq.com'):
        self.gh_id = gh_id
        self.pwd = pwd
        self.url = url
        self.session = utils.requests_retry_session()

    def get_session_with_cookie(self):
        data = utils.get_post_data(self.gh_id, self.pwd)
        resp = self.session.post(self.url + '/cgi-bin/bizlogin?action=startlogin', data=data, headers=utils.headers)
        res = resp.json()

        if (res['base_resp']['err_msg'] != 'ok'):
            print(res['base_resp']['err_msg'])
            return
        
        cookies = requests.utils.dict_from_cookiejar(resp.cookies)
        for key in cookies:
            self.session.cookies.set(key, cookies[key])

        redirect_url = self.url + res['redirect_url']

        return redirect_url, self.session